//
//  ZhuCeViewController.m
//  NewProject
//
//  Created by 王嘉琪 on 2017/6/6.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "ZhuCeViewController.h"
#import "UIButton+CodeDown.h"
@interface ZhuCeViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField* telTextField;
@property(nonatomic,strong)UITextField* yanzhengma;
@property(nonatomic,strong)UITextField* passWordField;
@property(nonatomic,strong)UITextField* againPassWordField;
@property(nonatomic,strong)UIButton* getYanZhengMa;
@property(nonatomic,strong)NSString* yanZhengNum;
@end

@implementation ZhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"注册";
    [self setUpView];
    // Do any additional setup after loading the view.
}
- (void)setUpView{
    self.view.backgroundColor=[UIColor whiteColor];
    _telTextField=[[UITextField alloc] initWithFrame:CGRectMake(20, 80, self.view.frame.size.width-40, 50)];
    _telTextField.delegate=self;
    _telTextField.clearButtonMode=YES;
    [_telTextField setFont:[UIFont systemFontOfSize:14]];
    _telTextField.placeholder = @"请输入手机号码";
    _telTextField.keyboardType=UIKeyboardTypePhonePad;
    _telTextField.layer.cornerRadius = 6.0f;
    [self.view addSubview:_telTextField];
    UIView *line1=[[UIView alloc] initWithFrame:CGRectMake(15, _telTextField.frame.size.height+_telTextField.frame.origin.y, _telTextField.frame.size.width, 1)];
    line1.backgroundColor=[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    [self.view addSubview:line1];
    
    _yanzhengma=[[UITextField alloc] initWithFrame:CGRectMake(20, line1.frame.origin.y+line1.frame.size.height, (self.view.frame.size.width-40)/3*2, 50)];
    _yanzhengma.delegate=self;
    _yanzhengma.clearButtonMode=YES;
    [_yanzhengma setFont:[UIFont systemFontOfSize:14]];
    _yanzhengma.placeholder = @"请输入短信验证码";
    _yanzhengma.layer.cornerRadius = 6.0f;
    [self.view addSubview:_yanzhengma];
    _yanzhengma.keyboardType=UIKeyboardTypePhonePad;
    UIView *line2=[[UIView alloc] initWithFrame:CGRectMake(15, _yanzhengma.frame.size.height+_yanzhengma.frame.origin.y, _telTextField.frame.size.width, 1)];
    line2.backgroundColor=[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    [self.view addSubview:line2];
    _getYanZhengMa = [[UIButton alloc] initWithFrame:CGRectMake(_yanzhengma.frame.size.width+_yanzhengma.frame.origin.x, _yanzhengma.frame.origin.y, (self.view.frame.size.width-40)/3, 50)];
    [_getYanZhengMa setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getYanZhengMa.titleLabel setFont:[UIFont systemFontOfSize:14]];
    _getYanZhengMa.titleLabel.font=[UIFont systemFontOfSize:12];
    [_getYanZhengMa setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_getYanZhengMa addTarget:self action:@selector(getYanZhengMa:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_getYanZhengMa];
    UIView *shuxian=[[UIView alloc] initWithFrame:CGRectMake(_getYanZhengMa.frame.origin.x, _getYanZhengMa.frame.origin.y+4, 1, 42)];
    shuxian.backgroundColor=[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    [self.view addSubview:shuxian];
    
    _passWordField=[[UITextField alloc] initWithFrame:CGRectMake(20, line2.frame.origin.y+line2.frame.size.height, self.view.frame.size.width-40, 50)];
    _passWordField.delegate=self;
    _passWordField.clearButtonMode=YES;
    [_passWordField setFont:[UIFont systemFontOfSize:14]];
    _passWordField.placeholder = @"请输入密码";
    _passWordField.secureTextEntry = YES;
    _passWordField.layer.cornerRadius = 6.0f;
    [self.view addSubview:_passWordField];
    UIView *line3=[[UIView alloc] initWithFrame:CGRectMake(15, _passWordField.frame.size.height+_passWordField.frame.origin.y, _passWordField.frame.size.width, 1)];
    line3.backgroundColor=[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    [self.view addSubview:line3];
    
    _againPassWordField=[[UITextField alloc] initWithFrame:CGRectMake(20, line3.frame.origin.y+line3.frame.size.height, self.view.frame.size.width-40, 50)];
    _againPassWordField.delegate=self;
    _againPassWordField.clearButtonMode=YES;
    _againPassWordField.secureTextEntry = YES;
    [_againPassWordField setFont:[UIFont systemFontOfSize:14]];
    _againPassWordField.placeholder = @"请输入确认密码";
    _againPassWordField.layer.cornerRadius = 6.0f;
    [self.view addSubview:_againPassWordField];
    UIView *line4=[[UIView alloc] initWithFrame:CGRectMake(15, _againPassWordField.frame.size.height+_againPassWordField.frame.origin.y, _againPassWordField.frame.size.width, 1)];
    line4.backgroundColor=[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    [self.view addSubview:line4];
    
    
    UIButton *next=[[UIButton alloc] initWithFrame:CGRectMake(20, line4.frame.size.height+line4.frame.origin.y+100, self.view.frame.size.width-40, 40)];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    next.backgroundColor=[UIColor colorWithRed:255/255.0 green:214/255.0 blue:1/255.0 alpha:1];
    next.clipsToBounds=YES;
    [next setTitleColor:[UIColor colorWithRed:21/255.0 green:21/255.0 blue:21/255.0 alpha:1] forState:UIControlStateNormal];
    [next.titleLabel setFont:[UIFont systemFontOfSize:14]];
    next.layer.cornerRadius=8;
    [next addTarget:self action:@selector(zhuCeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
    
    //注册
    UIButton *noMessage=[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3-30, next.frame.origin.y+next.frame.size.height+20, self.view.frame.size.width/4, 30)];
    noMessage.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    noMessage.titleLabel.font=[UIFont systemFontOfSize:12];
    [noMessage setTitle:@"收不到信息" forState:UIControlStateNormal];
//    [noMessage addTarget:self action:@selector(gotoZhuCe) forControlEvents:UIControlEventTouchUpInside];
    [noMessage setTitleColor:[UIColor colorWithRed:75/255.0 green:163/255.0 blue:181/255.0 alpha:1] forState:UIControlStateNormal];
    [noMessage.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:noMessage];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getYanZhengMa:(UIButton *)btn {
    [btn startCodeDownWithEndTitle:@"重新发送" cdoeTime:60 endClick:^{
        
    }];
    
    AFHTTPSessionManager*manager=[AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = nil;
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];
    
    NSString*urlstr=[NSString stringWithFormat:@"http://www.lnwuyou.com/sc/index.php?g=portal&m=index&a=send_yzm&tel=%@",_telTextField.text];
    //        NSDictionary * paramDic = @{@"tel":_telTextField.text,@"pass":_passWordField.text};
    [manager GET:urlstr parameters:nil  progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSLog(@"111%@",responseObject);
        NSDictionary*dic=[[NSDictionary alloc]init];
        dic=responseObject;
        if([dic[@"status"] isEqualToString:@"1"]){
                        NSLog(@"result = %@",dic);
                        _yanZhengNum=dic[@"code"];
                        NSLog(@"result:%@",dic[@"code"]);
                    }else{
                        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                                       message:dic[@"msg"] delegate:self
                                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                        [alert show];
                        NSLog(@"result:%@",dic[@"msg"]);
                    }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        NSLog(@"请求出错了: - %@ \n%@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }];
//    NSURLSession *session = [NSURLSession sharedSession];
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.lnwuyou.com/sc/index.php?g=portal&m=index&a=send_yzm&tel=%@",_telTextField.text]];
//    NSLog(@"%@",url);
//    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
//        if([dic[@"status"] isEqualToString:@"1"]){
//            NSLog(@"result = %@",dic);
//            _yanZhengNum=dic[@"code"];
//            NSLog(@"result:%@",dic[@"code"]);
//        }else{
//            NSLog(@"result:%@",dic[@"msg"]);
//        }
//        
//    }];
//    [task resume];
}
-(void)zhuCeBtn:(UIButton *)btn{
    if([_telTextField.text isEqualToString:@""]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"请输入手机号码" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if([_yanzhengma.text isEqualToString:@""]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"验证码不可为空" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if([_passWordField.text isEqualToString:@""]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"请输入密码" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if(![_yanzhengma.text isEqualToString:[NSString stringWithFormat:@"%@", _yanZhengNum]]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"验证码不正确，请重新输入" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if(![_passWordField.text isEqualToString:_againPassWordField.text]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"两次密码不一致，请重新输入" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        AFHTTPSessionManager*manager=[AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = nil;
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];
        
        NSString*urlstr=[NSString stringWithFormat:@"http://www.lnwuyou.com/sc/index.php?g=portal&m=index&a=register&tel=%@&pass=%@",_telTextField.text,_passWordField.text];
//        NSDictionary * paramDic = @{@"tel":_telTextField.text,@"pass":_passWordField.text};
        [manager GET:urlstr parameters:nil  progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
            NSLog(@"111%@",responseObject);
            NSDictionary*dic=[[NSDictionary alloc]init];
            dic=responseObject;
            if([dic[@"status"] isEqualToString:@"1"]){
                                NSLog(@"result = %@",dic);
                                NSLog(@"result:%@",dic[@"msg"]);
                                UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                                               message:@"注册成功" delegate:self
                                                                     cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                                [alert show];
                                NSLog(@"result:%@",dic[@"msg"]);
                            }else{
                                UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                                               message:dic[@"msg"] delegate:self
                                                                     cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                                [alert show];
                                NSLog(@"%@",dic);
                            }
            
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            NSLog(@"请求出错了: - %@ \n%@",
                  [error localizedDescription],
                  [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
        }];
//
    }
    
}
//键盘弹掉
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_passWordField resignFirstResponder];
    [_telTextField resignFirstResponder];
    [_yanzhengma resignFirstResponder];
    [_againPassWordField resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if(textField==_telTextField){
        if (textField.text.length == 11) {
            if ([string isEqualToString:@""]) {
                return YES;
            }
            else {
                return NO;
            }
        }
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
